#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MSTK.h"

#include "test.h"

void itoa(int n, char s[]);
void reverse(char s[]);

int main(int argc, char *argv[]) {
  int i, k, len, suff, ok, nv, ne;
  char mname[256], filename[256];
  Mesh_ptr mesh;
  /* default divide into two parts */
  int num = 2;
  int ring = 0;
  int verbose = 0;
  /* command line parameter */
  
  if (argc == 1) {
    fprintf(stdout, "Usage: meshpar -n 4 -r 0 filename\n");
    fprintf(stdout, "Option: -h for help\n");
    fprintf(stdout, "        -n number of partition\n");
    fprintf(stdout, "        -r number of ring\n");
    return -1;
  }
  argc--;
  argv++;
    
  while (argc >= 1)
    {
      if (argv[0][0] == '-') {	
	switch(argv[0][1]) {
	case 'h':
	case 'H':
	  fprintf(stdout, "Usage: meshpar -n 4 -r 0 filename\n");
	  fprintf(stdout, "Option: -h for help\n");
	  fprintf(stdout, "        -n number of partition\n");
	  fprintf(stdout, "        -r number of ring\n");
	  fprintf(stdout, "        -v verbose\n");
   	  argc -= 1;
          argv += 1;
	  return -1;
	  break;
	case 'n':
	case 'N':
	  num = atoi(argv[1]);
	  argc -= 2;
	  argv += 2;
          break;
	case 'v':
	case 'V':
	  verbose = 1;
	  argc -= 1;
	  argv += 1;
	  break;
	case 'r':
	case 'R':
	  ring = atoi(argv[1]);
	  argc -= 2;
	  argv += 2;
	  break;
	}
      }
      else {
	strcpy(mname,argv[0]);
	argc -= 1;
	argv += 1;
      }
    }
 
  fprintf(stderr,"\n");


  /* Initialize MSTK - Always do this even if it does not seem to
     matter in this version of MSTK */

  MSTK_Init();

  /* Load the mesh */

  len = strlen(mname);
  suff = 0;
  if (len > 5) {
    k = len-5;
    while (!suff && k > 1) {
      if (strncmp(&(mname[k]),".mstk",5) == 0) {
        suff = 1;
      }
      else
        k--;
    }
  }
  if (suff)
    mname[len-5] = '\0';

  strcpy(filename,mname);
  strcat(filename,".mstk");

  mesh = MESH_New(UNKNOWN_REP);
  ok = MESH_InitFromFile(mesh,filename);
  if (!ok) {
    fprintf(stderr,"Cannot open input file %s\n\n\n",filename);
    exit(-1);
  }

  MESH_BuildBoundary(mesh);
  strcpy(filename,mname);
  strcat(filename,"-build_boundary.mstk");
  PMESH_WriteToFile(mesh,filename,F1);

  /* Partition the mesh*/
  Mesh_ptr *submeshes = (Mesh_ptr *)MSTK_malloc(num*sizeof(Mesh_ptr));
  MESH_Partition(mesh,submeshes,num);
  char buf[256];
  char subfilename[256];


  for (i = 0 ; i < num ; i++) {
    itoa(i,buf);    
    /* for mstk2exo */
    strcpy(subfilename,mname);
    strcat(subfilename,buf);
    strcat(subfilename,".mstk");
    MESH_WriteToFile(submeshes[i],subfilename,F1);
  }

  for (i = 0 ; i < num ; i++) {
    itoa(i,buf);    
    MESH_BuildPType(mesh,submeshes[i]);
    strcpy(subfilename,mname);
    strcat(subfilename,buf);
    strcat(subfilename,"-build_ptype.mstk");
    PMESH_WriteToFile(submeshes[i],subfilename,F1);
  }
  for (i = 0 ; i < num ; i++) {
    itoa(i,buf);    
    MESH_AddGhost(mesh,submeshes[i],1);
    strcpy(subfilename,mname);
    strcat(subfilename,buf);
    strcat(subfilename,"-add_ghost.mstk");
    PMESH_WriteToFile(submeshes[i],subfilename,F1);
    /* for mstk2exo */
    strcpy(subfilename,mname);
    strcat(subfilename,buf);
    strcat(subfilename,"-with_ring.mstk");
    MESH_WriteToFile(submeshes[i],subfilename,F1);
  }  

  /* Deleting of mesh is not necessary if this the end of the program */
  for (i = 0; i < num; i++)
    MESH_Delete(submeshes[i]);
  /* Write out a copy of the mesh */

  strcpy(filename,mname);
  strcat(filename,"-copy.mstk");
  MESH_WriteToFile(mesh,filename,F1);

  return 1;
}

 void itoa(int n, char s[]) {
    int i, sign;
    sign = n;
    
    i = 0;
    do {
      s[i++] = abs(n % 10) + '0';
    } while ( n /= 10 );
    if (sign < 0)
      s[i++] = '-';
    
    s[i] = '\0';
    reverse(s);
  }
  void reverse(char s[]) {
    int c, i, j;
    for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
      c = s[i];
      s[i] = s[j];
      s[j] = c;
    }
  }  
